import 'package:flutter/cupertino.dart';
import 'package:squat_easy/se_constant.dart';
import 'package:squat_easy/se_login/se_login_logic.dart';
import 'package:squat_easy/se_net/se_global_url.dart';
import 'package:squat_easy/se_net/se_net.dart';
import 'package:squat_easy/se_route.dart';
import 'package:squat_easy/se_service/se_log_service.dart';
import 'package:squat_easy/se_ui/se_dialogs.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

class SeApiSetting extends SeApi {
  SeApiSetting.deleteAccount() : super(SeGlobalUrl.deleteAccount);
}

class SeSettingLogic with OnceClick {
  final ValueNotifier<bool> autoTranslate = ValueNotifier(false);

  void init() {
    autoTranslate.value = CommonStorage().getBool(SeStorageKey.spKeyAutoTranslate);
  }

  Future logout() async {
    SeLogManager.stat(page: SeLogPage.logout);
    try {
      startOnce(() async {
        await SeLogoutLogic.logout();
        SeRoute.openLogin();
        Loading.dismiss();
      }, clickMaskDismiss: false);
    } catch (e) {
      Loading.toast(e.toString());
    }
  }

  Future deleteAccount() async {
    SeLogManager.stat(page: SeLogPage.deleteAccount);
    SeDialogs.twoButton(
      title: 'Are you sure you want to delete your account?',
      confirmTitle: 'Delete',
      onConfirm: () async {
        try {
          startOnce(() async {
            await SeApiSetting.deleteAccount().post();
            SeLogoutLogic.clear();
            Loading.toast('Delete success', closeLoading: true);
            SeRoute.openLogin();
          }, clickMaskDismiss: false);
        } catch (e) {
          Loading.toast(e.toString());
        }
      },
    );
  }

  void onTaoAutoTranslate() async {
    autoTranslate.value = !autoTranslate.value;
    await CommonStorage().setBool(SeStorageKey.spKeyAutoTranslate, autoTranslate.value);
  }
}
